package com.derek.omshopping.ui.classify;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.derek.omshopping.R;
import com.derek.omshopping.base.BaseFragment;
import com.derek.omshopping.bean.classify.ClassifyLeftListViewBean;

import java.util.List;

import zxing.android.CaptureActivity;
import zxing.view.ViewfinderView;

import static cn.smssdk.utils.a.a;

/**
 * Created by asus on 2016/10/17 09 49.
 * 邮箱:763433963@qq.com
 * 该fragment用于实现分类页面
 */
public class ClassifyFragment extends BaseFragment implements ClassifyContract.View,View.OnClickListener {
    private ClassifyPresenter presenter;
    private ListView lv;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private String RightType_id;
    private ImageView iv;
    private Button btn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_classify, null);
        presenter = new ClassifyPresenter(this);
        presenter.getLeftTypeFromNet();

        btn= (Button) view.findViewById(R.id.btn_classify_search);
        btn.setOnClickListener(this);
        lv = (ListView) view.findViewById(R.id.lv_classify_left);
        iv= (ImageView) view.findViewById(R.id.iv_classify_two_dimensioncode);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), CaptureActivity.class);
                startActivity(intent);
            }
        });
        manager = getChildFragmentManager();
        transaction = manager.beginTransaction();
        transaction.add(R.id.fragment_layout, new ClassifyRightTypeFragment());
        transaction.commit();
        return view;
    }



    @Override
    protected void initData() {


    }

    @Override
    protected void initView() {


    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_classify;
    }

    @Override
    public void onListViewSuccess(final List<ClassifyLeftListViewBean> list_left, final List<String> code_mgroupAll) {

        //进行适配
        final ClassifyLeftListViewAdapter adapter = new ClassifyLeftListViewAdapter(list_left, getActivity());
        lv.setAdapter(adapter);
        Log.i("tag", "----------------------" + code_mgroupAll.size() + "-----------------");
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.changeSelected(position);

                String code_mgroup = code_mgroupAll.get(position);
                Log.i("tag", code_mgroup + "++++++++++++++++++++++++++++++++++++++++++");
                RightType_id=code_mgroup;
                Log.i("tag",RightType_id+"???????????????????????????????");
                ClassifyRightTypeFragment fragment=new ClassifyRightTypeFragment();
                Bundle bundle=new Bundle();
                bundle.putString("code_mgroup",code_mgroup);
                Log.i("tag",code_mgroup+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                fragment.setArguments(bundle);
                manager=getChildFragmentManager();
                transaction=manager.beginTransaction();
                transaction.replace(R.id.fragment_layout,fragment);
                transaction.commit();

            }

        });
        lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                adapter.changeSelected(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    @Override
    public void onListViewFail(String msg) {

    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent(getActivity(),ClassifySearchActivity.class);
        startActivity(intent);
    }
}

